package com.fhnew.app.ui.activity.userinfo.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.fhnew.app.R;
import com.fhnew.app.global.Constants;
import com.fhnew.app.model.CustomObserver;
import com.fhnew.app.model.internet.model.UserModel;
import com.fhnew.app.model.network.HttpJsonUtils;
import com.fhnew.app.model.network.entity.req.ResUserInfo;
import com.fhnew.app.ui.dialogactivity.PhoneDiolog;

import com.fhnew.app.util.SYSOUtils;

import butterknife.OnClick;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by Administrator on 2019/9/12.
 */

public class UserdataFragment extends Fragment {

    private TextView username;
    private EditText qqnumber;
    private EditText realname;
    private EditText wechatname;
    private Button usercommit;
    private ImageView phonetext;
    private TextView bangdingshouji;
    private TextView yibangding;
    private TextView bangdingbt;
    private ImageView messagetext;
    private TextView bangdingyouxiang;
    private String realnamestring;
    private String wechatnamestring;
    private String qqnamestring;



    private void initView (View view) {
        username = view.findViewById(R.id.username);
        qqnumber = view.findViewById(R.id.qqnumber);
        realname = view.findViewById(R.id.realname);
        wechatname = view.findViewById(R.id.wechatname);
        usercommit = view.findViewById(R.id.usercommit);
        phonetext = view.findViewById(R.id.phonetext);
        bangdingshouji = view.findViewById(R.id.bangdingshouji);
        yibangding = view.findViewById(R.id.yibangding);
        bangdingbt = view.findViewById(R.id.bangdingbt);
        messagetext = view.findViewById(R.id.messagetext);
        bangdingyouxiang = view.findViewById(R.id.bangdingyouxiang);
    }

    @Nullable
    @Override
    public View onCreateView (LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        container = (ViewGroup) inflater.inflate(R.layout.userdatalayout, null);
        initView(container);
        return container;
    }

    @Override
    public void onViewCreated (@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        try {

            username.setText(UserModel.user.userName);
            if (!TextUtils.isEmpty(UserModel.user.fullName)) {
                realname.setText(UserModel.user.fullName);
                realname.setFocusable(false);
            } else {
                realnamestring = realname.getText().toString().trim();
            }
            if (!TextUtils.isEmpty(UserModel.user.qq)) {
                qqnumber.setText(UserModel.user.qq);
                qqnumber.setFocusable(false);
            } else {
                qqnamestring = qqnumber.getText().toString().trim();
            }
            if (!TextUtils.isEmpty(UserModel.user.wechat)) {
                wechatname.setText(UserModel.user.wechat);
                wechatname.setFocusable(false);
            } else {
                wechatnamestring = wechatname.getText().toString().trim();
            }

            if (TextUtils.isEmpty(UserModel.user.isPhone)) {
                //未绑定手机号
                bangdingshouji.setVisibility(View.VISIBLE);
                yibangding.setText("未绑定");
                bangdingbt.setText("绑定");
                bangdingbt.setEnabled(true);
            } else {
                //已绑定手机号
                bangdingshouji.setVisibility(View.INVISIBLE);
                yibangding.setText("已绑定");
                bangdingbt.setText("已绑定");
                bangdingbt.setEnabled(false);
            }
        } catch (Exception e) {
        }
    }

    @Override
    public void onResume () {
        super.onResume();
        SYSOUtils.sOut_("用户信息+++++++++++++++++++++++++++++++++++" + UserModel.user);
        try {
            if (!TextUtils.isEmpty(UserModel.user.fullName)) {
                realname.setText(UserModel.user.fullName);
                realname.setFocusable(false);
            } else {
                realnamestring = realname.getText().toString().trim();
            }
            if (!TextUtils.isEmpty(UserModel.user.qq)) {
                qqnumber.setText(UserModel.user.qq);
                qqnumber.setFocusable(false);
            } else {
                qqnamestring = qqnumber.getText().toString().trim();
            }
            if (!TextUtils.isEmpty(UserModel.user.wechat)) {
                wechatname.setText(UserModel.user.wechat);
                wechatname.setFocusable(false);
            } else {
                wechatnamestring = wechatname.getText().toString().trim();
            }
            if (TextUtils.isEmpty(UserModel.user.isPhone)) {
                //未绑定手机号
                bangdingshouji.setVisibility(View.VISIBLE);
                yibangding.setText("未绑定");
                bangdingbt.setText("绑定");
                bangdingbt.setEnabled(true);
            } else {
                //已绑定手机号
                bangdingshouji.setVisibility(View.INVISIBLE);
                yibangding.setText("已绑定");
                bangdingbt.setText("已绑定");
                bangdingbt.setEnabled(false);
            }
        } catch (Exception e) {
        }
    }

    @OnClick({R.id.usercommit, R.id.bangdingbt})
    public void onViewClicked (View view) {
        switch (view.getId()) {
            case R.id.usercommit:
                if (!TextUtils.isEmpty(UserModel.user.fullName) && !TextUtils.isEmpty(UserModel.user.qq) && !TextUtils.isEmpty(UserModel.user.wechat)) {
                    usercommit.setFocusable(false);
                    usercommit.setEnabled(false);
                    Toast.makeText(getContext(), "请勿重复提交", Toast.LENGTH_SHORT).show();
                } else {

                    if (!TextUtils.isEmpty(UserModel.user.fullName)) {
                        realname.setText(UserModel.user.fullName);
                        realname.setFocusable(false);
                    } else {
                        if (!TextUtils.isEmpty(realname.getText().toString().trim())) {
                            realnamestring = realname.getText().toString().trim();
                        }
                    }
                    if (!TextUtils.isEmpty(UserModel.user.qq)) {
                        qqnumber.setText(UserModel.user.qq);
                        qqnumber.setFocusable(false);
                    } else {
                        if (!TextUtils.isEmpty(qqnumber.getText().toString().trim())) {
                            qqnamestring = qqnumber.getText().toString().trim();

                        }
                    }
                    if (!TextUtils.isEmpty(UserModel.user.wechat)) {
                        wechatname.setText(UserModel.user.wechat);
                        wechatname.setFocusable(false);
                    } else {
                        if (!TextUtils.isEmpty(wechatname.getText().toString().trim())) {
                            wechatnamestring = wechatname.getText().toString().trim();
                        }

                    }
                    HttpJsonUtils.getHttpApiInstance()
                            .upUserInfo(Constants.getuseAdress() + "api/m/upUserUserModel", realnamestring, qqnamestring, wechatnamestring, UserModel.Token)
                            .subscribeOn(Schedulers.io())
                            .observeOn(AndroidSchedulers.mainThread())
                            .subscribe(new CustomObserver<ResUserInfo>() {
                                @Override
                                protected void onCustomNext (ResUserInfo resUserUserModel) {
                                    if (resUserUserModel.code == 0) {
                                        Toast.makeText(getContext(), "修改成功", Toast.LENGTH_SHORT).show();
                                    } else {
                                        Toast.makeText(getContext(), resUserUserModel.msg + "", Toast.LENGTH_SHORT).show();
                                    }
                                }

                                @Override
                                protected void onSubscribeCustom (Disposable disposable) {
                                }

                                @Override
                                protected void onErrorCustom (Throwable e) {

                                }
                            });
                }
                break;
            case R.id.bangdingbt:
                Intent intent = new Intent(getContext(), PhoneDiolog.class);
                startActivity(intent);
                break;
        }
    }
}
